#!/bin/bash
##############################################################
# File Name：ftp_virtual_user
# Version :V1 . 
# Author: wangXiaoFeng 
# Email: 1289832464@qq.com
# Created Time ：2023-02-08 11:00:27
# Description：
#############################################################
ftp_maps_user=ftpuser
ftpDir=/data/ftp

while [ true ]; do
read -r -p "Please input ftp virtual_user name：" name
read -r -p "Please input ftp virtual_user passwd：" psd
cat /etc/vsftpd/virtual_user.txt |awk 'NR%2==1' |grep -w ${name}
if [ $? -eq '0' ];then
   echo "ftp virtual_user name [${name}] in The [/etc/vsftpd/virtual_user.txt] file already exists"
elif [[ $name != null && $psd != null && "$name" != "default" ]]; then
   echo $name >>/etc/vsftpd/virtual_user.txt
   echo $psd >>/etc/vsftpd/virtual_user.txt
   break
elif [[ "$name" == "default" ]]; then
    echo "Cannot add user with name 'default'"
else
    echo "Unknown exception, please try again"
	break
fi
done
rm -f /etc/vsftpd/vsftpd_login.db
db_load -T -t hash -f /etc/vsftpd/virtual_user.txt /etc/vsftpd/vsftpd_login.db

cp "/etc/vsftpd/virtual_user_conf/default" "/etc/vsftpd/virtual_user_conf/$name"
sed -i "s/default/${name}/"  /etc/vsftpd/virtual_user_conf/$name
mkdir -p ${ftpDir}/${name}
touch ${ftpDir}/${name}/${name}.md
chown -R ${ftp_maps_user}:${ftp_maps_user} ${ftpDir}
systemctl restart vsftpd
